package biz.impl;

import java.util.List;

import biz.CustomerBiz;
import dao.AddressDao;
import dao.CustomerDao;
import dao.OrderDao;
import dao.OrderDetailDao;
import dao.impl.AddressDaoImpl;
import dao.impl.CustomerDaoImpl;
import dao.impl.OrderDaoImpl;
import dao.impl.OrderDetailDaoImpl;
import entity.Customer;
import entity.Order;

public class CustomerBizImpl implements CustomerBiz{
	CustomerDao cd = new CustomerDaoImpl();
	AddressDao ad = new AddressDaoImpl();
	OrderDao od = new OrderDaoImpl();
	OrderDetailDao odd = new OrderDetailDaoImpl();
	@Override
	public Customer loginCustomer(String userName, String password) {
		Customer c =cd.queryCustomer(userName, password);
		if(c!=null){
			c.setAddressList(ad.queryAddressByCusId(c.getCusId()));
			c.setOrderList(od.queryOrderByCusId(c.getCusId()));
			List<Order> orderList = c.getOrderList();
			if(orderList!=null){
				for (Order order : c.getOrderList()) {
					order.setOrderDetail(odd.queryOrderDetailByOrderParentId(order.getOrderParentId()));
				}
			}
		}
		return c;
	}

	@Override
	public void registerCustomer(Customer customer) {
		cd.addCustomer(customer);
	}

	@Override
	public void updateCustomer(Customer customer) {
		cd.updateCustomer(customer);
	}

	@Override
	public Customer queryCustomerInfo(int customerId) {
		Customer c=cd.queryCustomer(customerId);
		if(c!=null){
			c.setAddressList(ad.queryAddressByCusId(customerId));
			c.setOrderList(od.queryOrderByCusId(customerId));
			List<Order> orderList = c.getOrderList();
			if(orderList!=null){
				for (Order order : c.getOrderList()) {
					order.setOrderDetail(odd.queryOrderDetailByOrderParentId(order.getOrderParentId()));
				}
			}
		}
		return c;
	}

	@Override
	public boolean queryCustomerExists(String userName) {
		return cd.queryCustomerExists(userName);
	}

	@Override
	public List<Customer> queryAllCustomer() {
		List<Customer> customerList = cd.queryAllCustomer();
		for (Customer c : customerList) {
			if(c!=null){
				c.setAddressList(ad.queryAddressByCusId(c.getCusId()));
				c.setOrderList(od.queryOrderByCusId(c.getCusId()));
				List<Order> orderList = c.getOrderList();
				if(orderList!=null){
					for (Order order : c.getOrderList()) {
						order.setOrderDetail(odd.queryOrderDetailByOrderParentId(order.getOrderParentId()));
					}
				}
			}
		}
		return customerList;
	}

}
